package com.dd.admin.business.sell.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.business.detailServer.entity.SellDetailServer;
import com.dd.admin.business.detailServer.service.SellDetailServerService;
import com.dd.admin.business.sellDetail.entity.SellDetail;
import com.dd.admin.business.sellDetail.service.SellDetailService;
import com.dd.admin.business.sellPay.domain.SellpayVo;
import com.dd.admin.business.sellPay.entity.Sellpay;
import com.dd.admin.business.sellPay.service.SellpayService;
import com.dd.admin.business.sellPay.service.impl.SellpayServiceImpl;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.mapper.SellMapper;
import com.dd.admin.business.sell.service.SellService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.admin.common.utils.ColumnUtil;
import com.dd.admin.common.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dd.admin.business.sell.domain.SellVo;
import com.dd.admin.business.sell.domain.SellDto;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-19
 */
@Service
public class SellServiceImpl extends ServiceImpl<SellMapper, Sell> implements SellService {

    @Autowired
    private SellDetailService sellDetailService;
    @Autowired
    private SellpayService sellpayService;
    @Autowired
    private SellDetailServerService sellDetailServerService;

    @Override
    public IPage<SellVo> selectSellPage(SellDto sellDto) {
        Page page = PageFactory.defaultPage();
        return baseMapper.selectSellPage(page,sellDto);
    }

    @Override
    public List<SellVo> selectSellList(SellDto sellDto) {
        return baseMapper.selectSellList(sellDto);
    }

    @Override
    public Integer selectTodayCount(String shopId) {
        return baseMapper.selectTodayCount(shopId);
    }

    @Override//TODO
    public void setSellDetail(List<SellVo> sellVoList) {
        List<String> sellIds = sellVoList.stream().map(SellVo::getSellId).collect(Collectors.toList());
        // 使用 MyBatis Plus 的条件构造器查询 SelldetailVo 列表

        List<SellDetail> sellDetailList = sellDetailService.selectSellDetailList(sellIds);
        List<Sellpay> sellpayList = sellpayService.selectSellPayList(sellIds);

        List<String> sellDetailIds = sellDetailList.stream().map(sellDetail -> sellDetail.getSellDetailId()).collect(Collectors.toList());
        List<SellDetailServer> sellDetailServers = sellDetailServerService.selectSellDetailServerListBySellDetailIds(sellDetailIds);
        // 获得所有的消费总单
        sellVoList.forEach(sellVo -> {
            List<SellDetail> selldetails = sellDetailList.stream()
                    .filter(selldetail -> sellVo.getSellId().equals(selldetail.getSellId()))
                    .collect(Collectors.toList());
            selldetails.stream().forEach(sellDetail -> {
                List<SellDetailServer> detailServerList = sellDetailServers.stream().filter(sellDetailServer -> sellDetail.getSellDetailId().equals(sellDetailServer.getSellDetailId())).collect(Collectors.toList());
                sellDetail.setDetailServerList(detailServerList);
            });
            sellVo.setSellDetailList(selldetails);

            List<Sellpay> sellpays = sellpayList.stream()
                    .filter(sellpay -> sellVo.getSellId().equals(sellpay.getSellId()))
                    .collect(Collectors.toList());
            sellVo.setSellPayList(sellpays);
        });
    }

    @Override
    public Integer selectSellCount(String shopId) {
        LambdaQueryWrapper<Sell> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Sell::getShopId, shopId);
        return baseMapper.selectCount(queryWrapper);
    }
}
